﻿using System;
using System.Collections.Generic;
using UnityEngine;

//角色管理
public class RoleMgr : Singleton<RoleMgr>
{

    int _mainRoleThisID;

    // thisId, role 
    public Dictionary<int, Role> AllRole = new Dictionary<int, Role>();

    internal static void OnMainRoleThisid(Cmd cmd)
    {
        if (!Nets.CheckCmd(cmd, typeof(MainRoleThisidCmd))) return;
        MainRoleThisidCmd thisidCmd = cmd as MainRoleThisidCmd;

        RoleMgr.instance._mainRoleThisID = thisidCmd.ThisID;



    }

    internal static void OnCreateSceneRole(Cmd cmd)
    {
        Debug.Log("OnCreateSceneRole " + cmd);
        if (!Nets.CheckCmd(cmd, typeof(CreateSceneRole))) return;
        CreateSceneRole createSceneRole = cmd as CreateSceneRole;


        RoleDatabase roleDatabase = RoleTable.instance[createSceneRole.ModelID];
        if (roleDatabase == null)
        {
            Debug.LogError("未找到角色模型：" + createSceneRole.ModelID);
            return;
        }

        //创建角色模型
        var roleObj = ResMgr.instance.GetInstance(roleDatabase.ModelPath);
        //GameObject.DontDestroyOnLoad(roleObj);
        Role role;
        //是否是主角
        if (instance._mainRoleThisID == createSceneRole.ThisID)
        {
            role = roleObj.AddComponent<MainRole>();
        }
        else
        {
            role = roleObj.AddComponent<Role>();
        }

        role.Init(createSceneRole, roleDatabase);

        RoleMgr.instance.AllRole[createSceneRole.ThisID] = role;

    }




}
